# -*- coding: utf-8 -*-
# Author: kimus

import gtk
from lvmman.gui import GladeDialog
import gobject
from pylvm import lvm
from pylvm.i18n import _

#import os
#import goocanvas
#import math

class LVCreateDialog(GladeDialog):

	MEASURE_UNITS = [
			{'name': _('Gigabytes'), 	'unit': 'G'},
			{'name': _('Megabytes'), 	'unit': 'M'},
			{'name': _('Terabytes'), 	'unit': 'T'},
			{'name': _('Petabytes'), 	'unit': 'P'},
			
			{'name': _('Extends'),   	'unit': ''},
			#{'name': _('Percentage'), 'type': '%'},
		]
	def __init__(self, vg):
		super(LVCreateDialog, self).__init__()
		self.vg = vg
		
		# create references to controls
		self.vgname = self.glade.get_widget('lblVGName')
		self.vgtotal = self.glade.get_widget('lblTotal')
		self.vgfree = self.glade.get_widget('lblFree')
		self.lvname = self.glade.get_widget('txtLVName')
		self.combounits = self.glade.get_widget('cboUnits')
		self.hssize = self.glade.get_widget('hsSize')
		self.lbunit = self.glade.get_widget('lblUnit')
		
		self.glade.signal_autoconnect({
				"on_butAdd_clicked"		:	self.start,
				"on_butClose_clicked"	:	self.close,
				"on_cboUnits_changed"	:	self.units_changed,
				None : None
			})

		self.initialize()

	def initialize(self):
		self.initialize_volume()
		self.initialize_units()
		
	def initialize_volume(self):
		self.vgname.set_text(self.vg.name)
		self.vgtotal.set_text(self.vg.total)
		self.vgfree.set_text(self.vg.free)
		
		(free_bytes, 
		 free_kilos, 
		 free_megas, 
		 free_gigas, 
		 free_teras,
		 free_petas
		) = self.vg.get_sizes(self.vg.free_extents)
		
		self.vg_units = {
				'B': free_bytes,
				'K': free_kilos,
				'M': free_megas,
				'G': free_gigas,
				'T': free_teras,
				'P': free_petas,
				'' : self.vg.total_extents, 
			}

		
	def initialize_units(self):
		cb = self.combounits
		
		lststore = gtk.ListStore(
				gobject.TYPE_STRING,
				gobject.TYPE_PYOBJECT
			)
		
		# create a CellRendererText to render the data
		cell = gtk.CellRendererText()

		# add the cell
		cb.pack_start(cell, True)

		# set the cell "text" attribute to column 1 - retrieve text
		# from that column in list store
		cb.add_attribute(cell, 'text',   0)		
		
		for unit in LVCreateDialog.MEASURE_UNITS:
			lststore.append( [unit['name'], unit] )

		cb.set_model(lststore)
		self.set_unit(0)
	
	def set_unit(self, index):
		cb = self.combounits
		cb.set_active(index)
		u = cb.get_model()[index][1]
		self.lbunit.set_text(u['unit'])
		self.hssize.set_range(0, self.vg_units[u['unit']])
		
		print self.vg_units[u['unit']], u['unit'] 
	
	def __startvalidate__(self):
		lvname = self.lvname.get_text()
		if len(lvname) <= 1:
			self.show_messagedialog(_("Error"), _("Please provide a LV Name!"), gtk.MESSAGE_ERROR)
			return False
		return True
			
	def __start__(self):
		cb = self.combounits
		idx = cb.get_active()
		u = cb.get_model()[idx][1]
		name = self.lvname.get_text()
		size = "%s%s" % (self.hssize.get_value(), u['unit'])

		lvm.create_lv(self.vg, name, size)
		
	# event handlers
	def units_changed(self, *args):
		self.set_unit(self.combounits.get_active())
